// mircoApp/new-auction/new_auction_recharge/new_auction_recharge.js
var yz_pay = require("../../../mycomponent/yz_pay/yz_pay");
const app = getApp();
Page({
  behaviors: [yz_pay],
  /**
   * 页面的初始数据
   */
  data: {
    priceVal: "",
    paymentList: [],
    money: "0.00",
    isClick: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    })
    this.getData();
    this.get_paymentList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 预下单
  async tapPay(e) {
    let {item} = e.currentTarget.dataset
    if (!this.data.isClick) {
      return;
    }
    this.data.isClick = false;
    let json = {
      new_auction_price: this.data.priceVal
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-auction.frontend.goods-buy"),
      data: json
    })

    if (data.result) {
      this.craeteOrder(item);
    } else {
      this.data.isClick = true;
      this.$toast(msg);
    }
  },
  async craeteOrder(item) {
    let json = {
      new_auction_price: this.data.priceVal
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-auction.frontend.create"),
      data: json
    })
    if (data.result) {
      this.getMergaPay(data.data.order_ids, item);
    } else {
      this.data.isClick = true;
      app.tips(data.msg)
    }
  },
  async getMergaPay(ids, btn) {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("order.merge-pay"),
      data: {
        order_ids: ids
      }
    })
    if (data.result) {
      this.data.order_pay_id = data.data.order_pay.id;
      this.data.pid = data.data.order_pay.uid;
      console.log(btn, btn.value) 
      this.handlePay(btn, btn.value);
      setTimeout(() => {
        this.data.isClick = true;
      }, 1000);
    } else {
      app.tips(msg);
      this.data.isClick = true;
    }
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-auction.frontend.money.memberInfo")
    })
    if (data.result) {
      this.setData({
        money: data.data.money
      })
    } else {
      app.tips(data.msg)
    }
  },
  async get_paymentList() {
    let json = {
      new_auction_price: this.data.priceVal > 0 ? this.data.priceVal : "0.01"
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-auction.frontend.goods-buy.paymentList"),
      data: json
    })
    if (data.result) {
      this.setData({
        paymentList: data.data
      })
    } else {
      app.tips(data.msg)
    }
  }
})